<template>
	<view class="order_container">
		<!-- tab -->
		<view class="order_top_tab_box">
			<view class="top_tab_box" v-for="(item, index) in topTabList" :key="index" :class="[isActive === index ? 'top_tab_active_box' : '']" @click="handlerTabs(item, index)">
				<span v-if="item.name == '分销订单'">{{ item.name }} | {{ orderTotalDistribution }}</span>

				<span v-else-if="item.name == '直营订单'">{{ item.name }} | {{ orderTotalDirectly }}</span>
			</view>
		</view>
		<view class="order_list_box">
			<view class="order_title_box">
				<tr v-for="(item, index) in orderTitleList" :key="index">
					<th>{{ item }}</th>
				</tr>
			</view>
			<view class="order_content_box" v-if="directOrder.length !== 0">
				<view class="order_content" v-for="(items, indexs) in directOrder" :key="indexs">
					<view class="order_all" @click="handlerMore(items, indexs)">
						<view style="width: 40rpx; word-break: break-all; margin-right: 20rpx">{{ indexs + 1 }}</view>
						<view style="width: 172rpx; word-break: break-all; margin-right: 20rpx">
							{{ items.created_at }}
						</view>
						<view style="width: 126rpx; word-break: break-all; margin-right: 8rpx">{{ items.express_name ? items.express_name : '暂无' }}</view>
						<view style="width: 180rpx; word-break: break-all">{{ items.mobile ? items.mobile : '暂无' }}</view>
						<view class="">
							<span v-if="items.order_status == 0">待支付</span>
							<span v-else-if="items.order_status == 11">已支付</span>
							<span v-else-if="items.order_status == 22">已发货</span>
							<span v-else-if="items.order_status == 33">已签收</span>
							<span v-else-if="items.order_status == 34">已激活</span>
							<span v-else-if="items.order_status == 44">已退款</span>
							<span v-else-if="items.order_status == 99">已取消</span>
						</view>
					</view>

					<view class="other_content_box" v-if="handlerFlag(items, indexs)">
						<view class="other_detail_top">
							<view class="detail_top_all">
								<view class="top_line"></view>
								<view class="top_title">订单详情</view>
							</view>
							<view class="detail_top_copy" @click="handleCopyOrder(items)">一键复制</view>
						</view>

						<view class="other_detail_con">
							<view class="other_order">订单编号：</view>
							<view class="other_con" style="font-family: my-font">
								{{ items.order_no }}
							</view>
						</view>

						<view class="other_detail_con">
							<view class="other_order">下单时间：</view>
							<view class="other_con">
								{{ items.created_at }}
							</view>
						</view>

						<view class="other_detail_con">
							<view class="other_order">订单状态：</view>
							<view class="other_con">
								<span v-if="items.order_status == 0">待支付</span>
								<span v-else-if="items.order_status == 11">已支付</span>
								<span v-else-if="items.order_status == 22">已发货</span>
								<span v-else-if="items.order_status == 33">已签收</span>
								<span v-else-if="items.order_status == 34">已激活</span>
								<span v-else-if="items.order_status == 44">已退款</span>
								<span v-else-if="items.order_status == 99">已取消</span>
							</view>
						</view>

						<view class="other_detail_con">
							<view class="other_order">套餐名称：</view>
							<view class="other_con">
								{{ items.good_name ? items.good_name : '暂无' }}
							</view>
						</view>

						<view class="other_detail_con">
							<view class="other_order">收货人：</view>
							<view class="other_con" style="font-family: my-font">
								{{ items.express_name }}
							</view>
						</view>

						<view class="other_detail_con">
							<view class="other_order">收货地址：</view>
							<view class="other_con">
								{{ items.express_address ? items.express_address : '无' }}
							</view>
						</view>

						<!-- 查看设备套餐 -->
						<view class="equipment_record_box">
							<view class="equipment_record" @click="handleCheckEquipmentRecord(items)">查看充值记录</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 暂无订单 -->
			<view class="order_content_no" v-else-if="directOrder.length == 0">———— 暂无订单 ————</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 顶部tabs
			topTabList: [
				{
					id: '0',
					name: '分销订单'
				},
				{
					id: '1',
					name: '直营订单'
				}
			],
			isActive: 0,
			orderTitleList: ['序', '下单时间', '姓名', '联系电话', '订单状态'],
			pageObj: {
				pageNow: 1,
				pageSize: 20
			},
			total: 0,
			orderTotalDistribution: 0, // 分销订单数量
			orderTotalDirectly: 0, // 直营订单那数量
			directOrder: [], // 直营订单
			distributionOrder: [] // 分销订单
		};
	},
	onShow() {
		this.getSystem();
		// this.getUserInfo()
		this.directOrder = [];
		this.distributionOrder = [];
		if (this.isActive == 0) {
			this.getDirectIotGoodsOrderList();
		} else {
			this.getFxIotGoodsOrderList();
		}

		if (this.pageObj.pageNow == this.total) {
			return;
		}
	},
	onReachBottom() {
		if (this.pageObj.pageNow < this.total) {
			this.pageObj.pageNow++;
			if (this.isActive == 0) {
				this.getDirectIotGoodsOrderList();
			} else {
				this.getFxIotGoodsOrderList();
			}
		} else if (this.pageObj.pageNow == this.total) {
			uni.showToast({
				title: '暂无更多数据',
				icon: 'none'
			});
			return;
		}
	},
	methods: {
		getSystem() {
			uni.request({
				url: 'https://saler.jkcae.com/app/getSystemSettingInfo',
				method: 'GET',
				success: (res) => {
					if (res.data.code == 1) {
						const pages = getCurrentPages();
						const page = pages[pages.length - 1];
						const title = page.$holder.navigationBarTitleText;
						uni.setStorageSync('systemMessage', res.data.data);
						uni.setNavigationBarTitle({
							title: res.data.data ? res.data.data.h5_title + '-' + title : '订单'
						});
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						return;
					}
				}
			});
		},

		// 获取用户信息
		getUserInfo() {
			this.$request(
				'/getUserInfo',
				{
					id: uni.getStorageSync('h5-userInfo').id
				},
				'POST'
			).then((res) => {
				if (res.code == 1) {
					this.personMsgObj = res.data;
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
					return;
				}
			});
		},

		// 获取设备直营订单
		getDirectIotGoodsOrderList() {
			this.$request(
				'/api/getDirectIotGoodsOrderList',
				{
					page_now: this.pageObj.pageNow,
					page_size: this.pageObj.pageSize
				},
				'GET'
			).then((res) => {
				if (res.code == 1) {
					res.data.forEach((item) => {
						this.directOrder.push(item);
					});
					this.orderTotalDistribution = res.page_info.total_count;
					this.total = res.page_info.page_count;
				} else {
					uni.showToast({
						title: '获取直营订单失败',
						icon: 'none'
					});
					return;
				}
			});
		},

		// 获取设备分销订单
		getFxIotGoodsOrderList() {
			this.$request(
				'/api/getFxIotGoodsOrderList',
				{
					page_now: this.pageObj.pageNow,
					page_size: this.pageObj.pageSize
				},
				'GET'
			).then((res) => {
				if (res.code == 1) {
					res.data.forEach((item) => {
						this.directOrder.push(item);
					});
					this.orderTotalDirectly = res.page_info.total_count;
					this.total = res.page_info.page_count;
				} else {
					uni.showToast({
						title: '获取分销订单失败',
						icon: 'none'
					});
					return;
				}
			});
		},

		// 切换tab
		handlerTabs(item, index) {
			this.isActive = index;
			this.directOrder = [];
			this.distributionOrder = [];
			if (this.isActive == 0) {
				this.getDirectIotGoodsOrderList();
			} else {
				this.getFxIotGoodsOrderList();
			}
		},

		// 点击显示更多
		handlerMore(val, index) {
			this.directOrder.forEach((item, index1) => {
				this.$set(item, `isShowMore${index1}`, false);
			});

			this.$set(val, `isShowMore${index}`, true);
		},

		handlerFlag(item, index) {
			return item['isShowMore' + index];
		},

		// 复制号卡订单
		handleCopyOrder(val) {
			let openStatus = '';
			if (val.order_status == 0) {
				openStatus = '待支付';
			} else if (val.order_status == 11) {
				openStatus = '已支付';
			} else if (val.order_status == 22) {
				openStatus = '已发货';
			} else if (val.order_status == 33) {
				openStatus = '已签收';
			} else if (val.order_status == 34) {
				openStatus = '已激活';
			} else if (val.order_status == 44) {
				openStatus = '已退款';
			} else if (val.order_status == 99) {
				openStatus = '已取消';
			}
			let orderObj =
				'订单编号：' +
				val.order_no +
				'\n' +
				'下单时间：' +
				(val.create_at ? val.create_at : '无') +
				'\n' +
				'订单状态：' +
				openStatus +
				'\n' +
				'套餐名称：' +
				val.good_name +
				'\n' +
				'收货人：' +
				val.express_name +
				'\n' +
				'收货地址：' +
				val.express_address;
			uni.setClipboardData({
				data: orderObj,
				success: function () {
					uni.showToast({
						title: '复制成功',
						icon: 'none',
						duration: 2000
					});
				}
			});
		},

		// 获取设备套餐充值记录
		getIotPackageByGoods(val) {
			this.$request(
				'/api/getIotPackageByGoods',
				{
					id: val
				},
				'GET'
			).then((res) => {
				if (res.code == 1) {
					res.data.forEach((item) => {
						this.recordList.push(item);
					});
					this.total = res.page_info.page_count;
				} else {
					uni.showToast({
						title: '查询充值记录失败',
						icon: 'none'
					});
					return;
				}
			});
		},

		// 查看充值记录
		handleCheckEquipmentRecord(val) {
			uni.navigateTo({
				url: '/pages/wifiOrder/equipmentRecord?id=' + val.id
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.order_container {
	padding: 16rpx 20rpx;

	.order_top_tab_box {
		display: flex;

		.top_tab_box {
			width: 50%;
			height: 68rpx;
			background: #ededed;
			line-height: 68rpx;
			text-align: center;
			font-size: 28rpx;
			font-weight: 500;
			font-family: screen-font;
			color: #333;
			border-radius: 8rpx 8rpx 0 0;
		}

		.top_tab_active_box {
			width: 50%;
			height: 68rpx;
			background: #4f96ff;
			line-height: 68rpx;
			text-align: center;
			font-size: 28rpx;
			font-family: screen-font;
			font-weight: 500;
			color: #fff;
			border-radius: 8rpx 8rpx 0 0;
		}
	}

	.order_type_box {
		display: flex;
		justify-content: flex-end;
		padding-bottom: 10rpx;

		.order_type {
			font-size: 30rpx;
		}
	}

	.order_screen_box {
		background: #ffffff;

		.order_screen_all {
			display: flex;
			// justify-content: space-between;
			align-items: center;
			border-radius: 4px;
			padding: 18rpx 24rpx 18rpx 28rpx;

			.screen_time_box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 20rpx 0 0;

				.screen_time {
					font-size: 26rpx;
					font-weight: 400;
					letter-spacing: 0px;
					line-height: 18.56rpx;
				}

				.time_icon {
				}
			}

			.screen_commodity_box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 20rpx 0 0;

				.screen_commodity {
					font-size: 26rpx;
					font-weight: 400;
					letter-spacing: 0px;
					line-height: 18.56rpx;
				}

				.commodity_icon {
				}
			}

			.screen_btn_box {
				display: flex;
				align-items: center;
				margin-left: 10rpx;

				.screen_btn {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 100rpx;
					height: 60rpx;
					line-height: 40rpx;
					font-size: 24rpx;
					font-family: button-font;
					font-weight: 500;
					color: #fff;
					background: #4f96ff;
					border-radius: 8rpx;
				}
			}
		}

		.screen_other_box {
			display: flex;
			flex-wrap: wrap;
			width: 100%;
			padding: 0 24rpx 18rpx 28rpx;

			.other_phone_box {
				width: 268rpx;
				margin: 0 18rpx 18rpx 0;
			}
		}
	}

	.order_list_box {
		border-radius: 4px;
		background: #ffffff;
		padding: 26rpx 22rpx 0 22rpx;
		margin-top: 16rpx;

		.order_title_box {
			display: flex;
			justify-content: space-between;
			font-family: all-font;
			padding-bottom: 20rpx;
		}

		.order_content_box {
			// display: flex;
			// justify-content: space-between;
			font-size: 22rpx;
			font-weight: 400;

			.order_content {
				.order_all {
					display: flex;
					font-family: all-font;
					border-top: 1px solid #f0f0f0;
					border-bottom: 1px solid #f0f0f0;
					padding: 22rpx 0;
				}

				.other_content_box {
					padding: 20rpx 0;
					background: #ededed;

					.other_detail_top {
						display: flex;
						align-items: center;
						justify-content: space-between;
						padding: 0 20rpx 16rpx 0;

						.detail_top_all {
							display: flex;
							align-items: center;

							.top_line {
								width: 6rpx;
								height: 26rpx;
								background: #ff5600;
								border-radius: 6rpx;
							}

							.top_title {
								color: #000;
								font-weight: 550;
								padding-left: 10rpx;
							}
						}

						.detail_top_copy {
							display: flex;
							align-items: center;
							height: 40rpx;
							background: linear-gradient(145deg, #b68a2c, #8d5e22);
							border-radius: 10rpx;
							font-weight: 400;
							font-family: screen-font;
							font-size: 24rpx;
							color: #fcfcfc;
							padding: 0 10rpx;
						}
					}

					.other_detail_con {
						display: flex;
						align-items: center;
						color: #000;
						font-size: 24rpx;
						padding: 0 0 10rpx 20rpx;

						.other_order {
							width: 170rpx;
							padding-right: 8rpx;
						}

						.other_con {
						}
					}

					.equipment_record_box {
						display: flex;
						justify-content: flex-end;

						.equipment_record {
							display: flex;
							align-items: center;
							justify-content: center;
							height: 53rpx;
							background: #fcfcfc;
							box-shadow: 0px 5rpx 6rpx 0px rgba(224, 224, 224, 0.77);
							border-radius: 27rpx;
							border: 1px solid #f5f5f5;
							font-family: button-font;
							font-weight: 400;
							font-size: 24rpx;
							color: #333333;
							padding: 0 10rpx;
							margin: 10rpx 20rpx 0 0;
						}
					}
				}
			}
		}

		.order_content_no {
			text-align: center;
			color: #ccc;
			padding-bottom: 20rpx;
		}
	}

	.commodity_text {
		color: #333;
		text-align: center;
		padding: 20rpx 0;
	}
}
</style>
